/*
 * $Id: RefCounted.h,v 1.3 2005-04-24 08:17:08 bacon Exp $
 */

#ifndef _XPP_REFCOUNTED_CLASS_
#define _XPP_REFCOUNTED_CLASS_

#include <xp/types.h>
#include <xp/macros.h>
#include <xp/bas/assert.h>
#include <xpp/Uncopyable.h>

namespace xpp
{
	class RefCounted: public Uncopyable
	{
	protected:
		RefCounted () 
		{
			ref_count = 0; 
		}

	public:
		virtual ~RefCounted () 
		{ 
			xp_assert (ref_count == 0);
		}

		void ref () const
		{
			ref_count++;	
		}
		void deref () const
		{
			if (--ref_count == 0) delete this;
		}

		xp_size_t count () const
		{
			return ref_count;
		}
		bool isShared () const
		{
			return ref_count > 1;
		}

	protected:
		mutable xp_size_t ref_count;
	};

}

#endif
